package com.lyy.lottery.service.impl;

import com.lyy.lottery.common.errorcode.ServiceErrorCodeConstants;
import com.lyy.lottery.common.exception.ServiceException;
import com.lyy.lottery.common.util.RegexUtil;
import com.lyy.lottery.controller.param.CreatePrizeParam;
import com.lyy.lottery.controller.param.PageParam;
import com.lyy.lottery.controller.param.UserRegisterParam;
import com.lyy.lottery.dao.dataobject.PrizeDo;
import com.lyy.lottery.dao.mapper.PrizeMapper;
import com.lyy.lottery.service.PictureService;
import com.lyy.lottery.service.PrizeService;
import com.lyy.lottery.service.dto.PageListDTO;
import com.lyy.lottery.service.dto.PrizeDTO;
import com.lyy.lottery.service.enums.UserIdentityEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class PrizeServiceImpl implements PrizeService {

    @Autowired
    private PictureService pictureService;
    @Autowired
    private PrizeMapper prizeMapper;

    @Override
    public Long createPrize(CreatePrizeParam param, MultipartFile picfile) {
        //1、保存上传的图片并保存到本机，返回文件名
        String fileName=pictureService.savePicture(picfile);
        //2、奖品信息存库--prize表，获取奖品id
        PrizeDo prizeDo=new PrizeDo();
        prizeDo.setName(param.getPrizeName());
        prizeDo.setDescription(param.getDescription());
        prizeDo.setImageUrl(fileName);
        prizeDo.setPrice(param.getPrice());

        prizeMapper.insert(prizeDo);

        //3、返回奖品id
        return prizeDo.getId();
    }

    @Override
    public PageListDTO findPrizeList(PageParam param) {
        //获取奖品记录数
        int total=prizeMapper.count();
        log.info("奖品种类数为：{}",total);
        //查询当前列表页
        List<PrizeDTO> prizeDTOList=new ArrayList<>();
        List<PrizeDo> prizeDos=prizeMapper.selectPrizeList(param.getOffset(),param.getPageSize());
        for (PrizeDo prizeDo:prizeDos){
            PrizeDTO prizeDTO=new PrizeDTO();
            prizeDTO.setPrizeId(prizeDo.getId());
            prizeDTO.setName(prizeDo.getName());
            prizeDTO.setPrice(prizeDo.getPrice());
            prizeDTO.setDescription(prizeDo.getDescription());
            prizeDTO.setImageUrl(prizeDo.getImageUrl());
            prizeDTOList.add(prizeDTO);
            log.info("奖品：{}",prizeDTO);
        }
        log.info("奖品列表：{}",prizeDTOList);
        return new PageListDTO(total,prizeDTOList);
    }
}






















